const DataObjectExtData = uni.getStorageSync('DataObjectExtData')
const util = require("./util.js");

function initialvaluestrans(dataObj, initialValues){
    initialValues.forEach((val3)=>{
      let type = val3.type
      let value = val3.value
      if(type=='value'){
        if(val3.initialValue == 'datetime1'){
          if(value == '#YEAR0101#'){
            const now = new Date(+new Date()+8*3600*1000)
            const formattedDate = now.toISOString().slice(0, 10)
            const firstDay = formattedDate.slice(0, 4) + '-01-01'
            dataObj[val3.initialValue] = [firstDay,formattedDate]
          }
        }else{
          if(value == "#TODAY#"){
            const now = new Date(+new Date()+8*3600*1000)
            const formattedDate = now.toISOString().slice(0, 10)
            dataObj[val3.initialValue] = formattedDate
          }else if(value == "#PARTNERCODE#"){
            dataObj[val3.initialValue] = uni.getStorageSync('partnerCode')
          }else if(value == "#PARTNERCODENAME#"){
            dataObj[val3.initialValue] = uni.getStorageSync('partnerCode') + ' ' + uni.getStorageSync('partnerName')
          }else if(value == "#USERNAME#"){
            dataObj[val3.initialValue] = uni.getStorageSync('userName')
          }else if(value == "#NOW#"){
            const now = new Date(+new Date()+8*3600*1000)
            dataObj[val3.initialValue] = now.toISOString().replace('T', ' ').slice(0, 19)
          }else if(value == "#NULL#"){
            dataObj[val3.initialValue] = null
          }else{
			dataObj[val3.initialValue] = value
		  }
        }
      }
    })
    return dataObj
}

function datastyletrans(datastyle){
	
}


function initialstyletrans(style, initialValues,expArgs){
	initialValues.forEach((val3)=>{
	  let type = val3.type
	  let value = val3.value
	  if(type=='value'){
		style.forEach((val,index)=>{
			if(val.name == val3.initialValue){
				if(style[index].style.editStyle === 'dddw' && style[index].style.dddw.displayColumn!==val.name){
					style[index].style.isneedtrans = true
				}
				if(value == "#TODAY#"){
					const now = new Date(+new Date()+8*3600*1000);
					const formattedDate = now.toISOString().slice(0, 10);
					style[index].style.colValue = formattedDate
					style[index].style.value = formattedDate
					style[index].style.coldisplay = formattedDate
					style[index].style.ismodified = true
				}else if(value == "#PARTNERCODE#"){
					style[index].style.colValue = uni.getStorageSync('partnerCode')
					style[index].style.value = uni.getStorageSync('partnerCode')
					style[index].style.coldisplay = uni.getStorageSync('partnerCode')
					style[index].style.ismodified = true
				}else if(value == "#PARTNERCODENAME#"){
					style[index].style.colValue = uni.getStorageSync('partnerCode') + ' ' + uni.getStorageSync('partnerName')
					style[index].style.value = uni.getStorageSync('partnerCode') + ' ' + uni.getStorageSync('partnerName')
					style[index].style.coldisplay = uni.getStorageSync('partnerCode') + ' ' + uni.getStorageSync('partnerName')
					style[index].style.ismodified = true
				}else if(value == "#USERNAME#"){
					style[index].style.colValue = uni.getStorageSync('userName')
					style[index].style.value = uni.getStorageSync('userName')
					style[index].style.coldisplay = uni.getStorageSync('userName')
					style[index].style.ismodified = true
				}else if(value == "#NOW#"){
					const now = new Date(+new Date()+8*3600*1000);;
					style[index].style.colValue = now.toISOString().replace('T', ' ').slice(0, 19);
					style[index].style.value = now.toISOString().replace('T', ' ').slice(0, 19);
					style[index].style.coldisplay = now.toISOString().replace('T', ' ').slice(0, 19);
					style[index].style.ismodified = true
				}else if (value == "#SYS_MDDM#") {
					let inival = uni.getStorageSync('mddm')
					style[index].style.colValue = inival
					style[index].style.value = inival
					style[index].style.coldisplay = inival
					style[index].style.ismodified = true
				}else if (value == "#SYS_CKDM#") {
					let inival = uni.getStorageSync('ckdm')
					style[index].style.colValue = inival
					style[index].style.value = inival
					style[index].style.coldisplay = inival
					style[index].style.ismodified = true
				}else if (value == "#HARDWARECODE#") {
					let inival = uni.getStorageSync('hardwareCode')
					style[index].style.colValue = inival
					style[index].style.value = inival
					style[index].style.coldisplay = inival
					style[index].style.ismodified = true
				}else if(value == '#YEAR0101#'){
					const now = new Date(+new Date()+8*3600*1000);;
					const formattedDate = now.toISOString().slice(0, 10);
					const firstDay = formattedDate.slice(0, 4) + '-01-01';
					style[index].style.colValue = firstDay
					style[index].style.value = firstDay
					style[index].style.coldisplay = firstDay
					style[index].style.ismodified = true
				}else if(value == '#NULL#'){
					style[index].style.colValue = null
					style[index].style.value = null
					style[index].style.coldisplay = ''
					style[index].style.ismodified = true
				}else{
					style[index].style.colValue = value
					style[index].style.value = value
					style[index].style.coldisplay = value
					style[index].style.ismodified = true
				}
			}
		  
		})
	  }else if(type == 'column'){
		let column = val3.column
		expArgs.forEach((expVal)=>{
		  if(expVal.colName == column.colName){
			var expValStr = expVal.value==undefined?expVal.colValue:expVal.value
			style.forEach((val,index)=>{
				if(val.name == val3.initialValue){
					style[index].style.colValue = expValStr
					style[index].style.value = expValStr
					style[index].style.coldisplay = expValStr
					style[index].style.ismodified = true
				}
			})
			
		  } 
		})
	  }
	})
	return style
}


function initialdatatrans(tempdata, initialValues, expArgs){ //每次扫码前 要赋初值
	initialValues.forEach((val3)=>{
		let type = val3.type
		let value = val3.value
		if(type=='value'){
			tempdata.forEach((hval)=>{
				hval.data.forEach((hval2)=>{
					if(hval2.name == val3.initialValue){
						if(value == "#TODAY#"){
							const now = new Date(+new Date()+8*3600*1000);;
							const formattedDate = now.toISOString().slice(0, 10);
							hval2.style.colValue = formattedDate
							hval2.style.value = formattedDate
							hval2.style.coldisplay = formattedDate
							hval2.style.ismodified = true
						}else if(value == "#PARTNERCODE#"){
							hval2.style.colValue = uni.getStorageSync('partnerCode')
							hval2.style.value = uni.getStorageSync('partnerCode')
							hval2.style.coldisplay = uni.getStorageSync('partnerCode')
							hval2.style.ismodified = true
						}else if(value == "#PARTNERCODENAME#"){
							hval2.style.colValue = uni.getStorageSync('partnerCode') + ' ' + uni.getStorageSync('partnerName')
							hval2.style.value = uni.getStorageSync('partnerCode') + ' ' + uni.getStorageSync('partnerName')
							hval2.style.coldisplay = uni.getStorageSync('partnerCode') + ' ' + uni.getStorageSync('partnerName')
							hval2.style.ismodified = true
						}else if(value == "#USERNAME#"){
							hval2.style.colValue = uni.getStorageSync('userName')
							hval2.style.value = uni.getStorageSync('userName')
							hval2.style.coldisplay = uni.getStorageSync('userName')
							hval2.style.ismodified = true
						}else if(value == "#NOW#"){
							const now = new Date(+new Date()+8*3600*1000);;
							hval2.style.colValue = now.toISOString().replace('T', ' ').slice(0, 19);
							hval2.style.value = now.toISOString().replace('T', ' ').slice(0, 19);
							hval2.style.coldisplay = now.toISOString().replace('T', ' ').slice(0, 19);
							hval2.style.ismodified = true
						}else if(value == '#YEAR0101#'){
							const now = new Date(+new Date()+8*3600*1000);;
							const formattedDate = now.toISOString().slice(0, 10);
							const firstDay = formattedDate.slice(0, 4) + '-01-01';
							hval2.style.colValue = firstDay
							hval2.style.value = firstDay
							hval2.style.coldisplay = firstDay
							hval2.style.ismodified = true
						}else if(value == '#NULL#'){
							hval2.style.colValue = null
							hval2.style.value = null
							hval2.style.coldisplay = ''
							hval2.style.ismodified = true
						}else{
							hval2.style.colValue = value
							hval2.style.value = value
							hval2.style.coldisplay = value
							hval2.style.ismodified = true
						}
					}
				})
			})
		}else if(type == 'column'){
			let column = val3.column
			expArgs.forEach((expVal)=>{
				if(expVal.colName == column.colName){
					var expValStr = expVal.value==undefined?expVal.colValue:expVal.value
					tempdata.forEach((hval)=>{
						hval.data.forEach((hval2)=>{
							if(val3.initialValue == hval2.name){
								hval2.style.colValue = expValStr
								hval2.style.value = expValStr
								hval2.style.coldisplay = expValStr
								hval2.style.ismodified = true
							}
						})
					})
				} 
			})
		}
	})
	console.log('tempdata',JSON.parse(JSON.stringify(tempdata)))
	console.log('initialValues',JSON.parse(JSON.stringify(initialValues)))
	return tempdata
}


function datalisttrans(datalist, datalisttemp,blockstyle,valtype,divarea){
	datalisttemp.forEach((val,index) => {
		var listtemp = []
		blockstyle.forEach((val2, index2) => {
			var style = val2.style
			var obj = {}
			obj['opttype'] = valtype
			obj['divArea'] = divarea
			style.forEach((val3, index3,origin) => {
				if(val3.style.editStyle === 'dddw' && val3.style.dddw.name=='d_nvo_uipage_dwc'){
					var tag_dddwName = val3.style.tag_dddwName
					var tabval =  val[val3.name]
					var customlist = []
					var DataObjectExtData = uni.getStorageSync('DataObjectExtData')
					for(var i=0; i<DataObjectExtData.length;i++){
						if(DataObjectExtData[i].dataobjectname == tag_dddwName && DataObjectExtData[i].coldata ==tabval){
							origin[index3].style['value'] = DataObjectExtData[i].coldata
							origin[index3].style['coldisplay'] = DataObjectExtData[i].coldisplay
							origin[index3].style['colValue'] = DataObjectExtData[i].coldisplay
						}
						if(DataObjectExtData[i].dataobjectname == tag_dddwName){
							customlist.push(DataObjectExtData[i])
							origin[index3].style['codeTable'] = customlist
						}
					}
				}else if(val3.style.editStyle === 'ddlb'){
					var codeTable = val3.style.codeTable
					var tabval =  val[val3.name]
					for(var i=0; i<codeTable.length;i++){
						if(codeTable[i].dataValue == tabval){
							origin[index3].style['value'] = tabval
							origin[index3].style['coldisplay'] = codeTable[i].displayValue
							origin[index3].style['colValue'] = tabval
						}
					}
				}else if(val3.name == 'urlfilename' || val3.name == 'urlfilenamex'){
					origin[index3].style['value'] = getApp().globalData.pic_address + val[val3.name]
					origin[index3].style['coldisplay'] = getApp().globalData.pic_address + val[val3.name]
					origin[index3].style['colValue'] = val[val3.name]
				}else if(val3.style.colType == "datetime" && val[val3.name]!=''&& val[val3.name]!=null){
					var datetran = util.formatdatecustom(new Date(Date.parse(val[val3.name])),val3.style.format)
					origin[index3].style['value'] = datetran
					origin[index3].style['coldisplay'] = datetran
					origin[index3].style['colValue'] = datetran
				}else if(val3.style.colType.indexOf('decimal') != -1 && val3.style.format!=''){
					var datetran = ''
					if(val[val3.name] !== null){
						datetran = util.formatNumber2(val[val3.name],  val3.style.format)
					}
					origin[index3].style['value'] = datetran
					origin[index3].style['coldisplay'] = datetran
					origin[index3].style['colValue'] = datetran
				}else{
					
					origin[index3].style['value'] = val[val3.name]
					origin[index3].style['coldisplay'] = val[val3.name]
					origin[index3].style['colValue'] = val[val3.name]
				}
			})
			obj['property'] = val2.property
			obj['blockName'] = val2.blockName
			obj['id'] = index2
			obj['blockid'] = index
			obj['data'] = JSON.parse(JSON.stringify(style))
			listtemp.push(JSON.parse(JSON.stringify(obj)))
		})
		datalist.push(listtemp)
	})
	return datalist
}


function replaceConditionstrans(dataObj, initialValues,style,expArgs){
	initialValues.forEach((val3) => {
		let type = val3.type
		let value = val3.value
		if (type == 'value') {
			if (val3.toReplace == 'datetime1') {
				if (value == '#YEAR0101#') {
					const now = new Date(+new Date()+8*3600*1000);;
					const formattedDate = now.toISOString().slice(0, 10);
					const firstDay = formattedDate.slice(0, 4) + '-01-01';
					dataObj[val3.toReplace] = [firstDay, formattedDate]
				}
			} else {
				if (value == "#TODAY#") {
					const now = new Date(+new Date()+8*3600*1000);;
					const formattedDate = now.toISOString().slice(0, 10);
					dataObj[val3.toReplace] = formattedDate
				} else if (value == "#PARTNERCODE#") {
					dataObj[val3.toReplace] = uni.getStorageSync('partnerCode')
				} else if (value == "#PARTNERCODENAME#") {
					dataObj[val3.toReplace] = uni.getStorageSync('partnerCode') + ' ' + uni.getStorageSync('partnerName')
				} else if (value == "#USERNAME#") {
					dataObj[val3.toReplace] = uni.getStorageSync('userName')
				}else if (value == "#SYS_MDDM#") {
					dataObj[val3.toReplace] = uni.getStorageSync('mddm')
				}else if (value == "#SYS_CKDM#") {
					dataObj[val3.toReplace] = uni.getStorageSync('ckdm')
				}else if (value == "#HARDWARECODE#") {
					dataObj[val3.toReplace] = uni.getStorageSync('hardwareCode')
				} else if (value == "#NOW#") {
					const now = new Date(+new Date()+8*3600*1000);;
					dataObj[val3.toReplace] = now.toISOString().replace('T', ' ').slice(0, 19);
				} else if (value == "#NULL#") {
					dataObj[val3.toReplace] = null
				}else{
					dataObj[val3.toReplace] = value
				}
			}
		}else if(type == 'stylearr'){
			if(val3.column.pageFrom==undefined){
				var styleKey = val3.styleKey
				dataObj[val3.toReplace] = style[styleKey.key][styleKey.default][styleKey.subkey]
			}
		}else if(type == 'column'){
			var colName= val3.column.colName
			if(val3.column.pageFrom==undefined){
				dataObj[val3.toReplace] = style[colName]
			}else{
				try{
					expArgs.forEach((hval)=>{
						hval.data.forEach((hval2)=>{
							if(colName == hval2.name ){
								dataObj[val3.toReplace] = hval2.style.colValue!==undefined?hval2.style.colValue:''
							}
						})
					})
				}catch(e){
					try{
						expArgs.forEach((expval)=>{
							if(expval.colName == colName){
								dataObj[val3.toReplace] = expval.value!==undefined? expval.value:''
							}
						})
					
					}catch(e){
						try{
							if(expArgs[colName]!=undefined){
								dataObj[val3.toReplace] = expArgs[colName]
							}
						}catch(e){}
					}
				}
				
			}
		}
	})
	return dataObj
}

function constranconditons(conditions,expArgs,temconditions){
	if(conditions != undefined){
		if(JSON.stringify(expArgs) != '[]' && JSON.stringify(conditions) != '[]' && JSON.stringify(conditions) != undefined){
			var temobj = {}
			for(var m=0; m<conditions.length; m++){
				for(var n=0; n<expArgs.length; n++){
					if(conditions[m].divArea == expArgs[n].divArea){
						var foreignKeyArr = conditions[m].foreignKeyArr
						foreignKeyArr.forEach((val)=>{
							if(expArgs[n].colName == val.baseKey){
								temobj[val.foreignKey] = expArgs[n].value
							}
							if(val.type === 'value'){
								temobj[val.foreignKey] = val.value 
							}
						})
						
					}
				}
			}
			temconditions[divArea] = temobj
		}
	}
	return temconditions
}

function defaultvaluesedit(values,datalist,setDefaultValuesRuleWhenEdit){
	console.log(values)
	values.forEach((val)=>{
			var key = val.setValue
			var type = val.type
			var value = val.value
			datalist[0].forEach((val2)=>{
				val2.data.forEach((val3)=>{
					if(val3.name == key){
						if(setDefaultValuesRuleWhenEdit == "isempty" && (val3.style.colValue === ''|| val3.style.colValue == null)){
							if(type == 'value'){
								if (value == "#TODAY#") {
									const now = new Date(+new Date()+8*3600*1000);;
									const formattedDate = now.toISOString().slice(0, 10);
									val3.style.colValue = formattedDate
									val3.style.coldisplay = formattedDate
									val3.style.value = formattedDate
								} else if (value == "#PARTNERCODE#") {
									val3.style.colValue  = uni.getStorageSync('partnerCode')
									val3.style.coldisplay = uni.getStorageSync('partnerCode')
									val3.style.value = uni.getStorageSync('partnerCode')
								} else if (value == "#PARTNERCODENAME#") {
									val3.style.colValue  = uni.getStorageSync('partnerCode') + ' ' + uni.getStorageSync('partnerName')
									val3.style.coldisplay = uni.getStorageSync('partnerCode') + ' ' + uni.getStorageSync('partnerName')
									val3.style.value = uni.getStorageSync('partnerCode') + ' ' + uni.getStorageSync('partnerName')
								} else if (value == "#USERNAME#") {
									val3.style.colValue  = uni.getStorageSync('userName')
									val3.style.coldisplay = uni.getStorageSync('userName')
									val3.style.value = uni.getStorageSync('userName')
								} else if (value == "#NOW#") {
									const now = new Date(+new Date()+8*3600*1000);
									val3.style.colValue  = now.toISOString().replace('T', ' ').slice(0, 19);
									val3.style.coldisplay = now.toISOString().replace('T', ' ').slice(0, 19);
									val3.style.value = now.toISOString().replace('T', ' ').slice(0, 19);
								}else if (value == "#NULL#") {
									val3.style.colValue  = null
									val3.style.coldisplay = ''
									val3.style.value = null
								}else {
									val3.style.colValue  = value
									val3.style.coldisplay = value
									val3.style.value = value
								}
							}
						}
					}
				})
			})
		})
	return datalist
}

function forcedefaultvaluesedit(values,datalist,expArgs){
	console.log(values)
	values.forEach((val)=>{
		var key = val.setValue
		var type = val.type
		var value = val.value
		if(type == 'value'){
			datalist.forEach((listVal)=>{
				listVal.forEach((val2)=>{
					val2.data.forEach((val3)=>{
						if(val3.name == key){
							if(value.indexOf("#TODAY#")!=-1){
								const now = new Date(+new Date()+8*3600*1000);;
								const formattedDate = now.toISOString().slice(0, 10);
								value = value.replace('#TODAY#',formattedDate)
							}
							if (value.indexOf("#PARTNERCODE#")!=-1) {
								value = value.replace("#PARTNERCODE#",  uni.getStorageSync('partnerCode'))
							}
							if (value.indexOf("#PARTNERCODENAME#")!=-1) {
								value = value.replace("#PARTNERCODENAME#",  uni.getStorageSync('partnerCode') + ' ' + uni.getStorageSync('partnerName'))
							} 
							if (value.indexOf("#USERNAME#")!=-1) {
								value = value.replace("#USERNAME#",  uni.getStorageSync('userName'))
							}
							if (value.indexOf("#NOW#")!=-1) {
								const now = new Date(+new Date()+8*3600*1000);;
								value = value.replace("#NOW#", now.toISOString().replace('T', ' ').slice(0, 19))
							}
							if (value == "#SYS_MDDM#") {
								value = value.replace("#SYS_MDDM#",uni.getStorageSync('mddm'))
							}
							if (value == "#SYS_CKDM#") {
								value = value.replace("#SYS_CKDM#",uni.getStorageSync('ckdm'))
							}
							if (value == "#HARDWARECODE#") {
								value =value.replace("#HARDWARECODE#", uni.getStorageSync('hardwareCode'))
							}
							if (value.indexOf("#NULL#")!=-1) {
								value = null
							}
							val3.style.colValue = value
							val3.style.coldisplay = value
							val3.style.value = value
							val3.style.ismodified = true
						}
					
					})
				})
			})
		}else if(type == 'column'){
			let column = val.column
			expArgs.forEach((expVal)=>{
				if(expVal.colName == column.colName){
					var expValStr = expVal.value==undefined?expVal.colValue:expVal.value
					datalist.forEach((listVal)=>{
						listVal.forEach((val2)=>{
							val2.data.forEach((val3)=>{
								if(val3.name == key){
									val3.style.colValue = expValStr
									val3.style.coldisplay = expValStr
									val3.style.value = expValStr
									val3.style.ismodified = true
								}
							})
						})
					})
				} 
			})
		}
		
	})
	console.log('forcedefaultvaluesedit',JSON.parse(JSON.stringify(datalist)))
	return datalist
}


module.exports = {
	initialvaluestrans,
	replaceConditionstrans,
	initialstyletrans,
	initialdatatrans,
	datastyletrans,
	defaultvaluesedit,
	forcedefaultvaluesedit,
	constranconditons,
	datalisttrans
}
